package owg.util.opengl;

import com.jogamp.opengl.GL3;

import owg.util.opengl.models.HPRF;
import owg.util.opengl.models.VertexF;
/**Provides an interface similar to OpenGL1's immediate mode.<br/>
 * This exists solely for the programmer's convenience and should only be used for debugging or 
 * the occasional, low-poly model where performance is not an issue.*/
public class GL3ImmediateModeWrapper implements ImmediateModeWrapper<GL3>
{
    private static int callCounter;

    private EGL3 egl;
    private HPRF model;
    private boolean retainModel;
    public GL3ImmediateModeWrapper(EGL3 egl)
    {
	this.egl = egl;
    }
    /**Begins a new primitive rendering sequence. 
     * The caller must specify which vertex attributes will be used.
     * The normal, color and texture coordinate states are reset to their default values upon a call to this method.*/
    public GLRenderable glBegin(boolean retainModel, int faceMode, 
	    int nVertexComps, boolean useNormal, int nColorComps, int nTexCoords) {
	model = egl.genModel("ImmediateMode"+(callCounter++), false, false, faceMode, nVertexComps, useNormal, nColorComps, nTexCoords, 0);
	this.retainModel = retainModel;
	return model;
    }

    public void glNormal3f(float x, float y, float z)
    {
	egl.attribs.currentNormal[0] = x;
	egl.attribs.currentNormal[1] = y;
	egl.attribs.currentNormal[2] = z;
    }
    public void glColor4f(float r, float g, float b, float a)
    {
	egl.setColor(r,g,b,a);
    }
    public void glTexCoord4f(float s, float t, float r, float q)
    {
	egl.attribs.currentTexCoord[0] = s;
	egl.attribs.currentTexCoord[1] = t;
	egl.attribs.currentTexCoord[2] = r;
	egl.attribs.currentTexCoord[3] = q;
    }

    public void glVertex3f(float x, float y, float z)
    {
	model.addIndex(model.getIndex());
	model.addVertex(
		new VertexF(new float[]{x,y,z}, egl.attribs.currentNormal.clone(), 
			egl.getActiveColor().clone(), egl.attribs.currentTexCoord.clone(), null));
    }
    public void glEnd()
    {
	model.end(egl);
	if(!retainModel)
	{
	    model.render();
	    model.dispose(egl);
	    model = null;
	}
    }
}
